import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api/request'
import user from './modules/user'

Vue.use(Vuex)

export default new Vuex.Store({
  modules:{
    user
  },
  state: {
    profile: null,         // 当前用户信息
    menu: {
      editableTabsValue: '0'   // 默认值先写死
    }
  },
  mutations: {
     addTabs(state, payload) {
      // 根据业务需求实现逻辑，例如：
      // state.tabs = [...state.tabs, payload]
    },
    SET_PROFILE (state, payload) {
      state.profile = payload;
      localStorage.setItem('userProfile', JSON.stringify(payload));
    },
    CLEAR_PROFILE (state) {
      state.profile = null
      localStorage.removeItem('userProfile');
    },
    SET_EDITABLE_TABS_VALUE(state, val) {
      state.menu.editableTabsValue = val
    },
  },
  actions: {
    // 拉取当前用户信息
    async fetchProfile ({ commit }) {
      const data = await api.get('/users/me')
      commit('SET_PROFILE', data)
      return data
    },
    // 退出登录
    logout ({ commit }) {
      localStorage.removeItem('token')
      commit('CLEAR_PROFILE')
    }
  }
})